package com.ruoyi.shop.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.order.api.util.RedisChar;
import com.ruoyi.shop.api.RemoteShopService;
import com.ruoyi.shop.api.domain.CheShop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.shop.mapper.CheCouponMapper;
import com.ruoyi.shop.api.domain.CheCoupon;
import com.ruoyi.shop.service.ICheCouponService;

/**
 * 优惠卷Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-03
 */
@Service
public class CheCouponServiceImpl implements ICheCouponService 
{
    @Autowired
    private CheCouponMapper cheCouponMapper;

    @Autowired
    private RemoteShopService remoteShopService;

    @Autowired(required = false)
    private RedisTemplate<Object,Object> redisTemplate;

    /**
     * 查询优惠卷
     * 
     * @param id 优惠卷主键
     * @return 优惠卷
     */
    @Override
    public CheCoupon selectCheCouponById(Long id)
    {
        if(redisTemplate!= null){
            Object o = redisTemplate.opsForValue().get(RedisChar.SHOP_COUPON_PREFIX + id);
            if(o != null){
                CheCoupon cheCoupon = JSON.parseObject(JSON.toJSON(o).toString(), CheCoupon.class);
                if(cheCoupon != null){
                    return cheCoupon;
                }
            }else{
                CheCoupon cheCoupon = cheCouponMapper.selectCheCouponById(id);
                redisTemplate.opsForValue().set(RedisChar.SHOP_COUPON_PREFIX + id, cheCoupon);
                return cheCoupon;
            }
        }
        return cheCouponMapper.selectCheCouponById(id);
    }

    /**
     * 查询优惠卷列表
     * 
     * @param cheCoupon 优惠卷
     * @return 优惠卷
     */
    @Override
    public List<CheCoupon> selectCheCouponList(CheCoupon cheCoupon)
    {
        List<CheCoupon> cheCoupons = cheCouponMapper.selectCheCouponList(cheCoupon);
        for (CheCoupon coupon : cheCoupons) {
            AjaxResult info = remoteShopService.getInfo(coupon.getShopId(), null);
            Object o = info.get(AjaxResult.DATA_TAG);
            coupon.setCheShop(JSON.parseObject(JSON.toJSON(o).toString(), CheShop.class));
            //如果当前时间大于结束时间
            if(coupon.getUseendTime().getTime() < System.currentTimeMillis()){
                coupon.setState(1L);
            }
        }

        return cheCoupons;
    }

    /**
     * 新增优惠卷
     * 
     * @param cheCoupon 优惠卷
     * @return 结果
     */
    @Override
    public int insertCheCoupon(CheCoupon cheCoupon)
    {
        return cheCouponMapper.insertCheCoupon(cheCoupon);
    }

    /**
     * 修改优惠卷
     * 
     * @param cheCoupon 优惠卷
     * @return 结果
     */
    @Override
    public int updateCheCoupon(CheCoupon cheCoupon)
    {
        if(redisTemplate!= null){
            redisTemplate.delete(RedisChar.SHOP_COUPON_PREFIX+cheCoupon.getId());
        }
        return cheCouponMapper.updateCheCoupon(cheCoupon);
    }

    /**
     * 批量删除优惠卷
     * 
     * @param ids 需要删除的优惠卷主键
     * @return 结果
     */
    @Override
    public int deleteCheCouponByIds(Long[] ids)
    {

        for (Long id : ids) {
            if(redisTemplate!= null){
                redisTemplate.delete(RedisChar.SHOP_COUPON_PREFIX+id);
            }
        }

        return cheCouponMapper.deleteCheCouponByIds(ids);
    }

    /**
     * 删除优惠卷信息
     * 
     * @param id 优惠卷主键
     * @return 结果
     */
    @Override
    public int deleteCheCouponById(Long id)
    {
        if(redisTemplate!= null){
            redisTemplate.delete(RedisChar.SHOP_COUPON_PREFIX+id);
        }
        return cheCouponMapper.deleteCheCouponById(id);
    }
}
